import { createRouter, createWebHistory } from "vue-router";
import { requestListMap } from "../utils/request";

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: "/",
      component: () => import("../views/home/index.vue"),
    },
    {
      path: "/about",
      component: () => import("../views/about/index.vue"),
    },
  ],
});

router.beforeEach((to, from, next) => {
  // 取消所有非目标路由的请求, 并删除key
  requestListMap.forEach(({ pathname, cancel }, key) => {
    // if (pathname !== to.path) {
    if (pathname===from.path) { // 如果是当前这个页面对应的请求, 就取消它
      cancel();
      requestListMap.delete(key);
    }
  });
  next();
});

export default router;
